package cn.new7as.cas5s.ui.repay;

import static cn.new7as.cas5s.tool.util.Utils.setLinerarHeight;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lxj.xpopup.util.XPopupUtils;

import java.util.List;

import cn.new7as.cas5s.R;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.ui.easy.netapi.OrderApi;
import cn.new7as.cas5s.ui.apply.ChangeForthActivity;

public class Ordersadapter extends BaseAdapter {

    private List<OrderApi.Data> list;
    private OrdersActivity context;


    public Ordersadapter(List<OrderApi.Data> list, OrdersActivity context) {
        super();

        this.context = context;
        this.list = list;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_grid_orders, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.pending = (TextView) convertView.findViewById(R.id.pending);
            viewHolder.enorderdetails = (LinearLayout) convertView.findViewById(R.id.enorderdetails);
            viewHolder.ramount = (TextView) convertView.findViewById(R.id.ramount);
          //  viewHolder.extendfee = (TextView) convertView.findViewById(R.id.extendfee);
            viewHolder.scheduledrepaymentdate = (TextView) convertView.findViewById(R.id.scheduledrepaymentdate);
            viewHolder.extend = (TextView) convertView.findViewById(R.id.extend);
            viewHolder.Settle = (TextView) convertView.findViewById(R.id.settle);
            viewHolder.update = (TextView) convertView.findViewById(R.id.update);
            viewHolder.ordname = (TextView) convertView.findViewById(R.id.ordname);
            viewHolder.scheduledrepaymentdate = (TextView) convertView.findViewById(R.id.scheduledrepaymentdate);
            viewHolder.subpaymnet = (RelativeLayout) convertView.findViewById(R.id.subpaymnet);
            viewHolder.llpayvis = (LinearLayout) convertView.findViewById(R.id.llpayvis);
            viewHolder.lpay = (LinearLayout) convertView.findViewById(R.id.lpay);
            viewHolder.layout = (LinearLayout) convertView.findViewById(R.id.layout);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.ramount.setText(list.get(position).predictTotalCapital);
       // viewHolder.extendfee.setText(list.get(position).hxnkxIftQkodIjxnyRcydy);
        viewHolder.scheduledrepaymentdate.setText(list.get(position).repayMentDate);
        viewHolder.ordname.setText(list.get(position).productCategoryName);
        int i = Utils.getordStaus(list.get(position));

        if (i == 1) {
            viewHolder.pending.setText(R.string.overdue);
            viewHolder.pending.setTextColor(Color.parseColor("#ff4242"));
            setLinerarHeight(viewHolder.layout, ViewGroup.LayoutParams.WRAP_CONTENT);
            viewHolder.llpayvis.setVisibility(View.VISIBLE);
        } else if (i == 2) {
            viewHolder.pending.setText("");
            viewHolder.llpayvis.setVisibility(View.VISIBLE);
            setLinerarHeight(viewHolder.layout, ViewGroup.LayoutParams.WRAP_CONTENT);
        } else if (i == 3) {
            setLinerarHeight(viewHolder.layout, XPopupUtils.dp2px(context, 200));
            viewHolder.llpayvis.setVisibility(View.GONE);
            viewHolder.pending.setText(R.string.pending);
            viewHolder.pending.setTextColor(Color.parseColor("#19c656"));

        } else if (i == 4) {
            viewHolder.pending.setText(R.string.rejected);
            viewHolder.pending.setTextColor(Color.parseColor("#0a0c06"));
            viewHolder.llpayvis.setVisibility(View.GONE);
            setLinerarHeight(viewHolder.layout, XPopupUtils.dp2px(context, 200));
        } else if (i == 5) {
            viewHolder.llpayvis.setVisibility(View.GONE);
            setLinerarHeight(viewHolder.layout, XPopupUtils.dp2px(context, 200));
            viewHolder.pending.setText(R.string.settled);
            viewHolder.pending.setTextColor(Color.parseColor("#5e6157"));
        } else if (i == 6) {
            viewHolder.llpayvis.setVisibility(View.VISIBLE);
            viewHolder.lpay.setVisibility(View.GONE);
            viewHolder.subpaymnet.setVisibility(View.GONE);
            setLinerarHeight(viewHolder.layout, XPopupUtils.dp2px(context, 279));
            if (list.get(position).bankStatus == 1)
                viewHolder.update.setVisibility(View.VISIBLE);
            viewHolder.pending.setText(R.string.payment_failed);
            viewHolder.pending.setTextColor(Color.parseColor("#5e6157"));
            if (list.get(position).bankStatus == 1)
                viewHolder.update.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Utils.go(context, ChangeForthActivity.class);
                    }
                });
        }


        viewHolder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, OrdersDetailActivity.class);
                Bundle bundle = new Bundle();
                list.get(position).fo = 1;
                bundle.putSerializable("ddba", list.get(position));
                intent.putExtras(bundle);
                context.startActivity(intent);


            }
        });
        viewHolder.subpaymnet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.go(context, SubProofActivity.class);
            }
        });
        viewHolder.extend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.getChanel(list.get(position), 2);
            }
        });
        viewHolder.Settle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.getChanel(list.get(position), 1);
            }
        });
        return convertView;
    }


    class ViewHolder {
        TextView pending;
        LinearLayout enorderdetails;
        LinearLayout llpayvis;
        LinearLayout lpay;
        LinearLayout layout;
        TextView ramount;
        TextView extendfee;
        TextView scheduledrepaymentdate;
        TextView extend;
        TextView Settle;
        TextView update;
        TextView ordname;
        RelativeLayout subpaymnet;
    }
}
